<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试JNDI数据源</title>
</head>
<body>

<%
	//获取容器对象
	InitialContext context=new InitialContext();
	//通过容器找到数据源连接池
	DataSource dataSource=(DataSource)context.lookup("java:comp/env/jdbc/news");
	//通过数据源获取连接对象
	Connection connection=dataSource.getConnection();
	out.println(connection+"<br>");
	String sql="select * from stu";
	PreparedStatement statement=connection.prepareStatement(sql);
	ResultSet resultSet=statement.executeQuery();
	while(resultSet.next()){
		out.println(resultSet.getInt(1)+"---"+resultSet.getString(2)+"---"+resultSet.getString(3)+"---"+resultSet.getString(4)+"---"+resultSet.getInt(5)+"<br>");
	}
	
%>



</body>
</html>